﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Globalization;
using University.Workshop.Presenters.WaterPurchase;

namespace University.Workshop.Presentation.WaterPurchase
{
    /// <summary>
    /// Interaction logic for PageSecondStepRegisterWaterPurchase.xaml
    /// </summary>
    public partial class PageSecondStepRegisterWaterPurchase : Page, ISecondStepForRegisterWaterPurchase
    {
        PresenterSecondStepForRegisterWaterPurchase pSecondStepForRegisterWaterPurchase;
        PresenterRegisterNewWaterPurchaseView pInternal;
        //Boolean primeraCarga; 

        public PageSecondStepRegisterWaterPurchase(PresenterRegisterNewWaterPurchaseView pExternal)
        {

            pInternal = pExternal;
            //primeraCarga = true;
            InitializeComponent();

            pSecondStepForRegisterWaterPurchase = new PresenterSecondStepForRegisterWaterPurchase(this);

            /*EventManager.RegisterClassHandler(typeof(DatePicker), DatePicker.LoadedEvent, new RoutedEventHandler(datePicker1_Loaded));

            System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-BO");
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("es-BO");*/
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            App.Current.MainWindow.Title = "Registrar Compra";
        }

        private void CantCupyPasteOrCut(object sender, CanExecuteRoutedEventArgs e)
        {
            if (e.Command == ApplicationCommands.Copy)
                e.Handled = true;
            if (e.Command == ApplicationCommands.Cut)
                e.Handled = true;
            if (e.Command == ApplicationCommands.Paste)
                e.Handled = true;
        }

        private void MountsTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            foreach (char ch in e.Text)
            {
                e.Handled = !(char.IsDigit(ch));
            }
        }

        private void MountsTxt_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }



        #region IRegisterBasicView Members

        public Model.WaterPurchase WaterPurchaseToRegister
        {
            get;
            set;
        }

        public void Update()
        {
            pSecondStepForRegisterWaterPurchase.UpdateData();
        }

        public string SystemMessage
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                pInternal.NotifySystemMessage(value);
            }
        }

        public string ErrorMessage
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                pInternal.NotifyErrorMessage(value);
            }
        }

        public void ChargeDataToBasic()
        {
            pSecondStepForRegisterWaterPurchase.ChargeDataUser();
        }

        public void GoNextPage()
        {
        }

        public void GoBackPage()
        {
            pInternal.GoStepOne();
        }

        #endregion


        #region ISecondStepForRegisterWaterPurchase Members

        public int Amount
        {
            get
            {
                if (AmountTxt.Text != "")
                    return int.Parse(AmountTxt.Text);
                else
                    return 0;
            }
            set
            {
                AmountTxt.Text = value.ToString();
            }
        }

        public DateTime DatePurchase
        {
            get
            {
                return DateTime.Parse(DatePurchaseDpk.Text);
            }
            set
            {
                if (value == DateTime.MinValue)
                    DatePurchaseDpk.Text = DateTime.Today.ToString();
                else
                    DatePurchaseDpk.Text = value.ToString();
            }
        }

        public bool Courtesy
        {
            get
            {
                return CourtesyChk.IsChecked.Value;
            }
            set
            {
                CourtesyChk.IsChecked = value;
            }
        }

        public decimal TotalPrice
        {
            get
            {
                if (TotalPriceTxt.Text != "")
                    return decimal.Parse(TotalPriceTxt.Text);
                else
                    return 0;

                //return decimal.Parse(TotalPriceTxt.Text);
            }
            set
            {
                TotalPriceTxt.Text = value.ToString();
            }
        }

        #endregion

        private void CourtesyChk_Checked(object sender, RoutedEventArgs e)
        {
            TotalPriceTxt.IsEnabled = false;
            TotalPriceTxt.Text = (0).ToString();
        }

        private void CourtesyChk_Unchecked(object sender, RoutedEventArgs e)
        {
            TotalPriceTxt.IsEnabled = true;
        }

        private void DatePurchaseDpk_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = true;
        }
    }
}
